package com.boarsoft.boar.code.mac.flow.action;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.code.agent.service.CodeService;
import com.boarsoft.boar.code.entity.WrapHandlerInfo;
import com.boarsoft.boar.code.mac.flow.biz.WrapHandlerInfoBiz;
import com.boarsoft.boar.code.mac.flow.generator.WrapHandlerGenerator;
import com.boarsoft.boar.code.mac.flow.generator.WrapMappingGenerator;
import com.boarsoft.boar.config.service.ConfigVarService;
import com.boarsoft.boar.context.ActionContext;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.rpc.core.RpcContext;

@RestController
@RequestMapping("/mac/flow/wrap")
public class WrapAction {
	private Logger log = LoggerFactory.getLogger(WrapAction.class);

	@Autowired
	private WrapHandlerInfoBiz wrapHandlerInfoBiz;

	@Autowired
	private WrapHandlerGenerator wrapHandlerGenerator;

	@Autowired
	private WrapMappingGenerator wrapMappingGenerator;

	@Autowired
	@Qualifier("developerCodeService")
	private CodeService developerCodeService;

	@Autowired
	@Qualifier("configVarSvc")
	private ConfigVarService configVarService;

	@RequestMapping("/save.do")
	// @Authorized(code = "flow.save")
	public ReplyInfo<Object> save(WrapHandlerInfo wrapHandlerInfo) {
		return new ReplyInfo<Object>(true, wrapHandlerInfoBiz.save(wrapHandlerInfo));
	}

	@RequestMapping("/get.do")
	public ReplyInfo<Object> get(String type, String key, String faceId, String faceCode, String methodId, String methodCode,
			String id, String code) {
		return ReplyInfo.SUCCESS;
	}

	@RequestMapping("/list.do")
	public ReplyInfo<Object> list(String appId, String faceId, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<WrapHandlerInfo> pr = wrapHandlerInfoBiz.list(key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, pr);
	}

	@RequestMapping("/submit.do")
	public ReplyInfo<Object> submit(int type, String code, String key, String appId, String projCode, HttpServletRequest request) {
		String ip = ActionContext.getRemoteIp(request);
		if (Util.strIsEmpty(ip)) {
			return new ReplyInfo<Object>("Can not get IP");
		}
		try {
			String java = "";
			if (type == 1) {
				if (Util.strIsEmpty(key)) {
					return new ReplyInfo<Object>("必须输入CommonWrapHandler的属性[key]值,规则：中心/流程id-节点id");
				}

				String[] group = StringUtils.split(key, "/");
				if (null == group || group.length != 2) {
					return new ReplyInfo<Object>("CommonWrapHandler的属性[key]值，不合法，规则：中心/流程id-节点id");
				}

				java = wrapMappingGenerator.generate(code, key, type, appId);
				if (configVarService.save(appId, group[0], key, java)) {
					return new ReplyInfo<Object>("保存失败，请重试！");
				}
			} else {
				RpcContext.specify2(ip.concat(":9941"));
				java = wrapHandlerGenerator.generate(code, key, type, appId);
				try {
					developerCodeService.write(projCode, code, java);
				} catch (Throwable e) {
					log.error("Can not write java source to remote server", e);
				}
			}
			return new ReplyInfo<>(true, java);
		} catch (Throwable e) {
			log.error("Error on generate code", e);
			return new ReplyInfo<Object>(e.getMessage());
		} finally {
			RpcContext.specify2(null);
		}
	}

}
